package com.imagine.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.imagine.common.Global;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map;

/**
 * Created by liangyue on 2017/10/9.
 */
@SuppressWarnings("unchecked")
public class BaseResult<T>  implements Serializable{

    @ApiModelProperty(value = "code : 返回代码，0表示OK，其它的都有对应问题")
    private int code = Global.Code.success;

    @ApiModelProperty(value = "message : 如果code!=0,错误信息")
    private String message = Global.Message.success;

    @JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
    @ApiModelProperty(value = "code为0时返回结果集")
    private T object = (T) new Object();

    @JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
    @ApiModelProperty(value = "附加信息")
    private Map<String, Object> map = new HashMap<>();

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getObject() {
        return object;
    }

    public void setObject(T object) {
        this.object = object;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public BaseResult(String errorMsg) {
        this.message = errorMsg;
        this.code = Global.Code.error;
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }

    public BaseResult() {
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }
    public void success() {
        this.code = Global.Code.success;
    }

    public void success(T object) {
        this.code = Global.Code.success;
        this.message = Global.Message.success;
        this.object = object;
        this.map = new HashMap<>();
    }

    public void error() {
        this.code = Global.Code.error;
        this.message = Global.Message.error;
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }

    public void error(String message) {
        this.code = Global.Code.error;
        this.message = message;
    }

    public void error(Global.ErrorEncode error) {
        this.code = error.getErrorNo();
        this.message = error.getName();
        this.object = (T) new Object();
        this.map = new HashMap<>();
    }
}